<?php
namespace app\admin\controller;
use app\admin\service\ShopService;
use think\Controller;
use \think\Db;
use think\Image;

use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/26
 * Time: 15:38
 */
/**
 * Class ShopConfig 商铺配置管理控制器
 * @package app\admin\controller
 */
class Shop extends Controller
{
    //店铺列表
    public function index()
    {
        $shopList = ShopService::instance()->getList();
        $this->assign('shopList', $shopList);
        return $this->fetch();
    }


    /**
     * 添加店铺
     * @return mixed
     */
    public function add()
    {
        $rs=ShopService::instance()->commodity();
        $staff=db::name('staff')->where('type',2)->select();
        $this->assign('staff',$staff);
        $this->assign('rs',$rs);
        return $this->fetch();
    }


    /**
     * 添加店铺--ajax提交
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addPost()
    {
        $oldShop = ShopService::instance()->getOneShop(input('shopName'));

        if($oldShop){
            return json(['code' => 1, 'msg' => '门店已存在']);
        }
        $re = ShopService::instance()->add();
        if($re){
            return json(['code' => 1, 'msg' => '添加成功']);
        }else{
            return json(['code' => -1, 'msg' => '添加失败']);
        }
    }

    /**
     * 编辑门店信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $shop = ShopService::instance()->getOneShop(input('shopId'));
        $rs=ShopService::instance()->commodity();
        foreach ($rs as $k => $v) {
            $rs[$k]['son']=db::name('shop_config')
                    ->where('commondityId',$v['id'])
                    ->where('shopId',input('shopId'))
                    ->field('shopMoney,discount')
                    ->find();
        }
        $staff=db::name('staff')->where('type',2)->select();
        $pic=db::name('shop_pic')->where('shopId',input('shopId'))->select();
        //dump(input('shopId'));exit;
        $this->assign('pic',$pic);       
        $this->assign('staff',$staff);
        $this->assign('rs',$rs);
        $this->assign('shop', $shop);
        return $this->fetch();
    }

    /**
     * 修改店铺信息提交
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPost()
    {
        $shop = ShopService::instance()->getOneShop(input('shopId'));
        if(!$shop){
            return json(['code' => -1, 'msg' => '店铺不存在']);
        }
        //dump(input());exit;
       $re = ShopService::instance()->edit(input('shopId'));
       return json($re);
    }

    /**
     * 修改门店状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update()
    {
        $re = ShopService::instance()->update();
        return json($re);
    }

    /**
     * 删除店铺
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del()
    {
        $re = ShopService::instance()->del();
        return json($re);
    }

    public function upload()
    {
        // $config = [
        //     'size' => 10000000,
        //     'ext'  => 'jpg,gif,png,bmp,jpeg,JPG'
        // ];
 
        // $file = $this->request->file('file');
 
        // $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
        // $save_path   = '/uploads/';
        // $info        = $file->validate($config)->move($upload_path);
 
        // $image = \think\Image::open(ROOT_PATH . 'public/'.$save_path . $info->getSaveName());
 
        // // 按照原图的比例生成一个最大为600*600的缩略图替换原图
        // $image->thumb(670, 313)->save(ROOT_PATH . 'public/'.$save_path . $info->getSaveName());
 
        // if ($info) {
        //     $res['code']=1;
        //     $res['msg'] = '上传成功！';
        //     $res['filepath'] = str_replace('\\', '/', $save_path . $info->getSaveName());
        // } else {
        //     $res['code'] = 0;
        //     $res['msg'] = '上传失败！'.$file->getError();
        // }
 
        // return json($res);



        if(request()->isPost()){
            $file = request()->file('file');
            // 要上传图片的本地路径
            $filePath = $file->getRealPath();
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            //获取当前控制器名称
            // $controllerName=$this->getContro();
            // 上传到七牛后保存的文件名
            $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
            require_once APP_PATH . '/../vendor/qiniu-php-sdk-7.2.2/autoload.php';
            // 需要填写你的 Access Key 和 Secret Key
            $accessKey = config('ACCESSKEY');
            $secretKey = config('SECRETKEY');
            // 构建鉴权对象
            $auth = new Auth($accessKey, $secretKey);
            // 要上传的空间
            $bucket = config('BUCKET');
            $domain = config('DOMAINImage');
            $token = $auth->uploadToken($bucket);
            // 初始化 UploadManager 对象并进行文件的上传
            $uploadMgr = new UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            if ($err !== null) {
                return ["err"=>1,"msg"=>$err,"data"=>""];
            } else {
                //返回图片的完整URL
                return json(["err"=>0,"msg"=>"上传完成","data"=>('http://prhy2y4ce.bkt.clouddn.com/'.$domain . $ret['key'])]);
            }
        }
    }
    /**
     * 删除图片
     * @return array
     */
    public function picDel(){
        $rs=db::name('shop_pic')->where('pic',input('pic'))->delete();
        if($rs){
            return json(['code' => 1, 'msg' => '成功']);
        }else{
            return json(['code' => -1, 'msg' => '删除失败请联系管理员']);
        }
    }




}
